package main;

import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.apache.xmlbeans.XmlException;

import ppt.pptToImage;
import ppt.pptxToImage;

public class Main extends JFrame {
	private JMenuBar setMenu;
	private JLabel setPPTText;
	private JTextField setPPTInputText;
	private JButton setPPTButton;
	private JLabel setInputText;
	private JTextField setInputInputText;
	private JButton setInputButton;

	private JButton setButton;

	public Main() {
		super();
		this.setSize(240, 200);
		this.getContentPane().setLayout(new FlowLayout());
		setLocationRelativeTo(null);
		this.setResizable(false);
		this.setTitle("PPT2PNG");
		this.add(setPPTText());
		this.add(setPPTInputText());
		this.add(setPPTButton());
		this.add(setInputText());
		this.add(setInputInputText());
		this.add(setInputButton());
		this.add(setButton());
		// 监听
		ActionListener();

	}

	// ppt转换文件选择
	private javax.swing.JLabel setPPTText() {
		if (setPPTText == null) {
			setPPTText = new javax.swing.JLabel();
			setPPTText.setText("请选择要转换的ppt文件");
		}
		return setPPTText;
	}

	private javax.swing.JTextField setPPTInputText() {
		if (setPPTInputText == null) {
			setPPTInputText = new javax.swing.JTextField();
			setPPTInputText.setEditable(false);
			setPPTInputText.setColumns(12);
		}
		return setPPTInputText;
	}

	private javax.swing.JButton setPPTButton() {
		if (setPPTButton == null) {
			setPPTButton = new javax.swing.JButton();
			setPPTButton.setText("选择文件");
		}
		return setPPTButton;
	}

	// 输出路径选择
	private javax.swing.JLabel setInputText() {
		if (setInputText == null) {
			setInputText = new javax.swing.JLabel();
			setInputText.setText("请选择输出路径");
		}
		return setInputText;
	}

	private javax.swing.JTextField setInputInputText() {
		if (setInputInputText == null) {
			setInputInputText = new javax.swing.JTextField();
			setInputInputText.setEditable(false);
			setInputInputText.setColumns(12);
		}
		return setInputInputText;
	}

	private javax.swing.JButton setInputButton() {
		if (setInputButton == null) {
			setInputButton = new javax.swing.JButton();
			setInputButton.setText("选择路径");
		}
		return setInputButton;
	}

	// 开始转换
	private javax.swing.JButton setButton() {
		if (setButton == null) {
			setButton = new javax.swing.JButton();
			setButton.setText("开始转换");
		}
		return setButton;
	}

	private void ActionListener() {
		//监听关闭事件
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		// 选择ppt文件按钮
		setPPTButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent event) {
				// 设置资源选择器
				JFileChooser jfc = new JFileChooser(new File(""));
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				jfc.showDialog(new JLabel(), "选择");
				File file = jfc.getSelectedFile();
				try {
					System.out.println("文件:" + file.getAbsolutePath());
					setPPTInputText().setText(file.getAbsolutePath());
					System.out.println(jfc.getSelectedFile().getName());
				} catch (Exception e) {
					System.out.println("未选择任何文件!");
				}
			}
		});
		// 选择输出文件夹按钮
		setInputButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent event) {
				// 设置资源选择器
				JFileChooser jfc = new JFileChooser(new File(""));
				jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				jfc.showDialog(new JLabel(), "选择");
				File file = jfc.getSelectedFile();
				try {
					System.out.println("文件夹:" + file.getAbsolutePath());
					setInputInputText().setText(file.getAbsolutePath());
					System.out.println(jfc.getSelectedFile().getName());
				} catch (Exception e) {
					System.out.println("未选择任何文件夹!");
				}
			}
		});
		// 开始转换按钮
		setButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent event) {
				if ("".equals(setPPTInputText().getText())) {
					// 弹出提示
					JOptionPane.showMessageDialog(null, "请选择文件！", "标题", JOptionPane.PLAIN_MESSAGE);
				} else if ("".equals(setInputInputText().getText())) {
					// 弹出提示
					JOptionPane.showMessageDialog(null, "请选择文件夹！", "标题", JOptionPane.PLAIN_MESSAGE);
				}
				String fileName[] = new String(setPPTInputText().getText()).split("\\.");
				String format = fileName[(fileName.length) - 1];
				if ("ppt".equals(format)) {
					pptToImage pti = new pptToImage();
					try {
						int idx = pti.toImg(setInputInputText().getText(), setPPTInputText().getText());
						JOptionPane.showMessageDialog(null, "转换成功" + idx + "页", "转换结果！", JOptionPane.PLAIN_MESSAGE);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else if ("pptx".equals(format)) {
					pptxToImage pti = new pptxToImage();
					try {
						int idx = pti.toImg(setInputInputText().getText(), setPPTInputText().getText());
						JOptionPane.showMessageDialog(null, "转换成功" + idx + "页", "转换结果！", JOptionPane.PLAIN_MESSAGE);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
	}

	public static void main(String[] args) {
		Main main = new Main();
		main.setVisible(true);
	}
}
